#!/usr/bin/env python

import errno, os, sys

if __name__ == '__main__':
    sys.path.append(
            os.path.dirname(os.path.dirname(os.path.abspath(sys.argv[0]))))

import maitch
import mcommon

from maitch import opj

__already_run = False

HUFF2C = "${SRC_DIR}/libhdvbd/huff2c.py"

def init(ctx):
    global __already_run
    if __already_run:
        return
    __already_run = True
    if ctx.mode == 'configure':
        pass
    elif ctx.mode == 'build':

        my_builddir = opj(ctx.build_dir, "libhdvbd")
        ctx.ensure_out_dir(opj(my_builddir, "gst"))
        try:
            os.symlink(".", opj(my_builddir, "hdvb"))
        except OSError as e:
            if e.errno != errno.EEXIST:
                raise

        # gudev flags/libs include gobject's
        cflags = "${CFLAGS} ${SQLITE_CFLAGS} ${GUDEV_CFLAGS} " + \
                mcommon.hdvbd_includes
        libs = "${LIBS} ${SQLITE_LIBS} ${GUDEV_LIBS}"
        gst_cflags = cflags + " ${GSTREAMER_CFLAGS}"
        gst_libs = libs + " ${GSTREAMER_LIBS}"

        # Gobs
        gobs = ctx.glob_src("*.gob", "libhdvbd", False)
        c_from_gobs = maitch.change_suffix(gobs, ".gob", ".c")
        gst_gobs = ctx.glob_src("*.gob", "libhdvbd/gst", False)
        gst_c_from_gobs = maitch.change_suffix(gst_gobs, ".gob", ".c")
        all_c_from_gobs = c_from_gobs + gst_c_from_gobs

        # How to compile gobs
        for g in gobs + gst_gobs:
            t = []
            for s in [".c", ".h", "-private.h"]:
                t += maitch.change_suffix(g, ".gob", s)
            ctx.add_rule(maitch.Rule(rule = "${GOB2} -o ${TGT_DIR} ${SRC}",
                    sources = g, targets = t))

        # Generated Huffman tables
        generated_huff_c = ["libhdvbd/freesathuff1.c",
                "libhdvbd/freesathuff2.c"]
        for n in [1, 2]:
            ctx.add_rule(maitch.Rule(
                    rule = "python " + HUFF2C + " ${SRC} ${TGT}",
                    sources = "libhdvbd/freesat.t%d" % n,
                    targets = "libhdvbd/freesathuff%d.c" % n,
                    deps = HUFF2C))

        c_files = ctx.glob_src("*.c", "libhdvbd", False) + \
                c_from_gobs + generated_huff_c
        o_files = maitch.add_prefix(
                maitch.change_suffix(c_files, ".c", ".lo"), "libhdvbd-")

        gst_c_files = gst_c_from_gobs
        gst_o_files = maitch.add_prefix(
                maitch.change_suffix(gst_c_files, ".c", ".lo"), "libhdvbd-gst-")

        # How to compile the C to .lo for a shlib. Note how wdeps forces all
        # files built by gob2 to be built first
        for c in c_files:
            ctx.add_rule(maitch.ShlibCRule(
                    sources = c,
                    cflags = cflags,
                    prefix = "libhdvbd-",
                    wdeps = all_c_from_gobs + generated_huff_c,
                    quiet = True))
        for c in gst_c_files:
            ctx.add_rule(maitch.ShlibCRule(
                    sources = c,
                    cflags = gst_cflags,
                    prefix = "libhdvbd-gst-",
                    wdeps = all_c_from_gobs,
                    quiet = True))

        # The actual libraries
        ctx.add_rule(maitch.CShlibRule(
                sources = o_files,
                targets = "libhdvbd/libhdvbd.la",
                cflags = cflags,
                libs = libs,
                quiet = True))
        ctx.add_rule(maitch.CShlibRule(
                sources = gst_o_files,
                targets = "libhdvbd/libhdvbd-gst.la",
                cflags = gst_cflags,
                libs = gst_libs,
                quiet = True))



if __name__ == '__main__':
    ctx = mcommon.init(PACKAGE = "libhdvbd",
            BUILD_DIR = os.path.abspath(opj(os.path.dirname(sys.argv[0]),
                    os.pardir, "build")))
    init(ctx)
    ctx.run()

